﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using 枸杞.Models.Entities;
using static System.Net.Mime.MediaTypeNames;

namespace 枸杞
{
    public static class StaticClass
    {
        private static string GetAssemblyDirectory() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "";
        internal static void Run(this bool b, Action action)
        {
            if (b) action();
        }

        internal static T? Run<T>(this bool b, Func<T> action)
        {
            if (b) return action();
            return default;
        }

        internal static Tout? Run<Tin, Tout>(this bool b, Func<Tin, Tout> action, Tin i)
        {
            if (b) return action(i);
            return default;
        }
#if NET6_0_OR_GREATER

        public static string GetResourceTextstr(string 文件名)
        {
            var path = Path.Combine(GetAssemblyDirectory(), 文件名);
            if (!File.Exists(path))
            {
                return "";
            }
            using StreamReader reader = new(path);
            return reader.ReadToEnd();
        }

        public static ObservableCollection<Code> GetTrees(this ObservableCollection<Code> codes, long parentId = 0)
        {
            ObservableCollection<Code> nodes = new(codes.Where(x => x.ParentID == parentId));
            foreach (Code node in nodes)
            {
                node.ChildNodes = codes.GetTrees(node.ID);
            }
            return nodes;
        }

#endif
    }
}